﻿
using NicoView.Model;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace NicoView.ViewModel
{
    public class NicoViewCommentCollection : ObservableCollection<NicoViewCommentViewModel>
    {

        public NicoViewCommentCollection()
        {

            if (!NicoViewViewModelBase.IsInDesignModeStatic)
            {
                NicoViewApplication.Current.Property.PropertyChanged += onPropertyChanged;
            }
            
        }

        ~NicoViewCommentCollection()
        {
            if (!NicoViewViewModelBase.IsInDesignModeStatic)
            {
                NicoViewApplication.Current.Property.PropertyChanged -= onPropertyChanged;
            }
        }




        protected override void InsertItem(int index, NicoViewCommentViewModel item)
        {
            item.Comment.PropertyChanged += onPropertyChanged;
            base.InsertItem(index, item);
        }

        

        protected override void RemoveItem(int index)
        {
            if (base.Count > index)
            {
                base[index].PropertyChanged -= onPropertyChanged;
            }

            base.RemoveItem(index);
        }

        private void onPropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            Upgrade();
        }


        public void Upgrade()
        {
            
            OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset) { });
        }
    }
}
